package learn;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Map;

/**
 * @author yxc
 * @description: 这里注意 启动类如果 直接放在默认的java文件夹下 会报错 Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
 * 原因：SpringBoot在编写启动类（Main方法所在的类）的时候如果不使用@ComponentScan指明对象扫描范围，默认指扫描当前启动类所在的包里的对象。
 * 解决：https://www.cnblogs.com/gudi/p/7892769.html
 * 方法一：添加一层包（已选用)
 * 方法二：添加注解 ComponentScan 指定要扫描的包
 * @date 2021/12/22
 */

@SpringBootApplication
@ServletComponentScan(basePackages = {"learn.servlet"})
@MapperScan("learn.mapper")
public class MainApplication {
    public static void main(String[] args) {

        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
 /*       // 查看容器中的组建
        String[] names = run.getBeanDefinitionNames();
           for(String name:names){
               System.out.println(name);
           }
           System.out.println("===============");
           User user1 = run.getBean("user01", User.class);
           User user2 = run.getBean("user01", User.class);
           // @Bean默认 单实例
            System.out.println("@Bean实例化的对象");
           System.out.println(user1 == user2);
        // proxyBeanMethods - true 总会检查 这个组件在容器中是否已存在，保持单例
        MyConfig myConfig = run.getBean(MyConfig.class);
        System.out.println("myConfig实例化的对象");
        User user3 = myConfig.user01();
        User user4 = myConfig.user01();
        System.out.println(user3 == user4);
        System.out.println("===============");
        Pets pets = run.getBean("tom",Pets.class);
        System.out.println(pets.toString());
        System.out.println(pets);
        System.out.println(user1.getPets().toString());
        System.out.println(user1.getPets());
        System.out.println(pets == user1.getPets());*/


//        String[] beanNamesForType = run.getBeanNamesForType(DBHelper.class);
//        for(String name:beanNamesForType){
//            System.out.println(name);
//        }
//        System.out.println("容器组件数" + run.getBeanDefinitionCount());
//        System.out.println("存在webMvc:" +run.getBeanNamesForType(WebMvcProperties.class).length);
//        DBHelper dbHelper = run.getBean(DBHelper.class);
//        System.out.println(dbHelper);
//        // 判断容器中是否有 tom组件
//        System.out.println("容器中是否有tom组件："+run.containsBean("tom"));
//        System.out.println("容器中是否有user01组件："+run.containsBean("user01"));
//
//        new Car("111",12);


        ConfigurableEnvironment environment = run.getEnvironment();
        // 系统环境变量 和                属性
        Map<String,Object> systemEnv = environment.getSystemEnvironment();
        Map<String,Object> systemProperties = environment.getSystemProperties();
        System.out.println(systemEnv);
        System.out.println(systemProperties);
    }
}
